package io.renren.modules.live.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import io.renren.modules.live.entity.FanweVideoPropEntity;
import io.renren.modules.live.vo.UserAnchorUpdateInfoVo;
import io.renren.modules.live.vo.UserListVo;
import io.renren.modules.live.vo.getVideoProdMoneyByTimeAndIdDTO;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.live.dao.FanweUserDao;
import io.renren.modules.live.entity.FanweUserEntity;
import io.renren.modules.live.service.FanweUserService;

@Log4j2
@Service("fanweUserService")
public class FanweUserServiceImpl extends ServiceImpl<FanweUserDao, FanweUserEntity> implements FanweUserService {

    @Autowired(required = false)
    FanweUserDao fanweUserDao;
    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 监听队列用户减少礼物收入
     * @throws IOException
     */
    @RabbitListener(queues = "exchange.user.bonus")
    public void consume(Message message, Channel channel) throws IOException {
        //FanweVideoPropEntity propEntity = (FanweVideoPropEntity) rabbitTemplate.receiveAndConvert("exchange.user.bonus");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);//手动确认
        String msgJson = new String(message.getBody(),"UTF-8");
        byte[] body = message.getBody();
        String json = new String(body,"UTF-8");
        JSONObject jsonObject1 = JSONObject.parseObject(json);
        Long userId = jsonObject1.getLong("userId");
        BigDecimal propMoney = jsonObject1.getBigDecimal("propMoney");
        log.info("收到消息: {}", message.toString());
        //数据库用户减少增加礼物收入金额
        try {
            fanweUserDao.updateAnchorMoney(userId,propMoney);
            System.out.println("用户减少增加礼物收入金额成功");
        }catch (Exception e){
            log.info(e);
            System.out.println("用户减少增加礼物收入金额失败");
        }
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FanweUserEntity> page = this.page(
                new Query<FanweUserEntity>().getPage(params),
                new QueryWrapper<FanweUserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public FanweUserEntity queryByUserName(String username) {
        return baseMapper.queryByUserName(username);
    }

    /**
     * 管理员查看用户列表
     * @param userListVo
     * @return
     * @throws Exception
     */
    @Override
    public Map<String , Object> selectUserList(UserListVo userListVo) throws Exception {
        Map<String , Object> map=new HashMap<>();
        userListVo.setPageIndex((userListVo.getPageIndex()-1)*userListVo.getPageSize());
        List<FanweUserEntity> fanweUserEntities = fanweUserDao.selectUserList(userListVo);
        Integer integer = fanweUserDao.selectUserListCont(userListVo);
//        for(FanweUserEntity fanweUserEntity : fanweUserEntities){
//
//        }
        map.put("fanweUserEntitieList",fanweUserEntities);
        map.put("cout",integer);
        return map;
    }

    /**
     * 通过ID查询用户信息
     * @param userId
     * @return
     */
    @Override
    public FanweUserEntity selectUserByid(Long userId) {
        return fanweUserDao.selectUserByid(userId);
    }

    @Override
    public void updatePwd(Long userId, String newPwd) {
        fanweUserDao.updatePwd(userId,newPwd);
    }

    @Override
    public void updatePayPwd(Long userId, String newPwd) {
        fanweUserDao.updatePayPwd( userId,  newPwd);
    }

    @Override
    public void addUserFile(Long userId,String fileUrl) {
        fanweUserDao.addUserFile(userId,fileUrl);
    }

    @Override
    public void saveUser(FanweUserEntity fanweUserEntity) {
        fanweUserDao.saveUser(fanweUserEntity);
    }

    @Override
    public void updateUserById(UserAnchorUpdateInfoVo userAnchorUpdateInfoVo) {
        fanweUserDao.updateUserById(userAnchorUpdateInfoVo);
    }

    @Override
    public List<getVideoProdMoneyByTimeAndIdDTO> getUserByIdSet(Integer videoId, String time) {
        return fanweUserDao.getUserByIdSet(videoId,time);
    }


}